/* ===================================================
*
*       Filename:  show_mem.c
*
*
*        Created:  10/15/2011 09:38:40 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  durand_y, <durand.yoan@gmail.com>
*
* ===================================================
*/

#include "malloc.h"

s_map   *g_map_list;

static void	            unsigned_rec(long unsigned int		n,
					 long unsigned int		base)
{
	long unsigned int	num = 0;
	char		c = '0';
	char		tab[6] = {'a', 'b', 'c', 'd', 'e', 'f'};

	if (n == 0)
		return;

	if (n > 0)
	{
		num = (n % base) + c;
		if (base == 16)
		{
			if ((num > '9'))
				num -= c;
			if ((num > 9) && (num < 16))
				num = tab[num - 10];
		}
	}
	else
	{
		num = (-n % base) + c;
	}
		unsigned_rec(n / base, base);
		write(STDERR_FILENO, &num, 1);

}

static void		        print_unsigned(long unsigned int	n,
		                               long unsigned int	base)
{
	char	c = '0';

	if (n == 0)
	{
		n = n + c;
		write(STDERR_FILENO, &n, 1);
	}
	unsigned_rec(n, base);
}

void                    my_show_mem(void)
{
    s_map   *map = NULL;
    s_block *bl = NULL;
    void    *ptr = NULL;
    char    *s = NULL;

    map = g_map_list;
    while (map != NULL)
    {
        bl = map->block;
        while (bl != NULL)
        {
            write(STDERR_FILENO, "0x0000", 6);
            print_unsigned((long unsigned int)bl->end, 16);
            write(STDERR_FILENO, "-0x0000", 7);
            ptr = bl->end;
            s = ptr;
            s += bl->size;
            print_unsigned((long unsigned int)s , 16);
            write(STDERR_FILENO, "\n", 1);
            bl = bl->next;
        }
        map = map->next_map;
    }
}
